package com.douqu.game.core.config.map;

import com.douqu.game.core.config.common.CommonData;
import com.douqu.game.core.entity.GameObject;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.LoadFactory;
import com.douqu.game.core.util.LoadUtils;
import com.douqu.game.core.util.LogUtils;
import com.douqu.game.core.util.time.TimeData;

import java.util.*;

/**
 * @Author: Bean
 * @Description:
 * @Date: 2018-09-03 18:06

public int count;

public int routeId;
 */
public class CityExpeditionConfig extends GameObject {

    public transient TimeData startTime;

    public int armyType;

    public int count;

    /**
     * 要刷新的远征军列表
     * id 为阵营
     * value为路线ID
     */
    public CommonData[] routes;

    @Override
    public boolean check()
    {
        if(routes == null)
        {
            LogUtils.error("Load CityExpeditionConfig check Error -> routs is null! id:" + id);
            return false;
        }
        if(startTime == null)
        {
            LogUtils.error("Load CityExpeditionConfig check Error -> startTime is null! id:" + id);
            return false;
        }

        Map<Integer, List<Integer>> map = new HashMap<>();
        List<Integer> list = null;
        for(CommonData commonData : routes)
        {
            if(DataFactory.getInstance(LoadFactory.isTestLoad()).getGameObject(DataFactory.ROUTE_KEY, commonData.value) == null)
            {
                LogUtils.error("Load CityExpeditionConfig check Error -> Route is null! value:" + commonData.value + "  id:" + id);
                return false;
            }

            list = map.get(commonData.id);
            if(list == null)
            {
                list = new ArrayList<>();
                map.put(commonData.id, list);
            }
            if(list.contains(commonData.value))
            {
                LogUtils.error("Load CityExpeditionConfig route repeated -> " + routes + " id:" + id);
                return false;
            }

            list.add(commonData.id);
        }
        return super.check();
    }



    @Override
    public boolean setVariable(String key, String value)
    {
        if("startTime".equals(key))
        {
            startTime = LoadUtils.loadTimeData(key, value);
            if(startTime == null)
            {
                LogUtils.error("Load CityExpeditionConfig Error -> startTime:" + value + "  id:" + id);
                return false;
            }
            return true;
        }
        else if("routes".equals(key))
        {
            routes = LoadUtils.loadCommonDataToArray(key, value);
            if(routes.length == 0)
            {
                LogUtils.error("Load CityExpeditionConfig Error -> routes:" + value + "  id:" + id);
                return false;
            }
            return true;
        }
        else
            return super.setVariable(key, value);
    }

    @Override
    public String toString() {
        return "{" +
                "startTime=" + startTime +
                ", armyType=" + armyType +
                ", count=" + count +
                ", routes=" + Arrays.toString(routes) +
                "}-" + super.toString();
    }
}
